<?php
 
namespace Admin\Controller;

// +----------------------------------------------------------------------
// | 文件 MallGoodsCdkeysController.class.php
// +----------------------------------------------------------------------
// | 说明 商品的激活码管理
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class MallGoodsCdkeysController extends BaseManageController {


    protected $modelName = 'Common/MallGoodsCdkeys';
    protected $modelType = 1; 
    /**
    *删除类型的设置 true|直接删除， false|设置数据状态
    */  
    protected $is_del   = true;

    protected $text_map = array(
        'status' => array(0 => '未购买', '1' => '已购买', '2' => '已兑换'),
    ); 

    protected function _initialize()
    {
        parent::_initialize();
    }

    public function _before_index() {

        if (!I('goods_id', 0, 'intval')){
            $this->error('商品信息错误');
        }
        $this->assign('goods_id', I('goods_id'));
        $this->where = [
            'goods_id' => I('goods_id', 0, 'intval')
        ];
        $this->sort = "status asc";
    }

    public function format_lists(&$lists) {
        $ids = array_column($lists, 'id');
        $ids && $key_user_list = D('MallUserCdkeys')->where(['status' => 1, 'cdkey_id' => ['in', $ids]])->getField('cdkey_id,uid', true);
        if (!empty($key_user_list)) {
            $map = [
                'id' => ['in', array_map('intval', array_values($key_user_list))]
            ];
            $users = service('Users')->where($map)->field('id,nickname')->select();
            $users = $users ? array_column($users, null, 'id') : [];

            foreach ($lists as &$value) {
                $user = $users[$key_user_list[$value['id']]];
                $user && $value['user'] = $user;
            }
        }
    }
    
    public function import($goods_id)
    {
        if (IS_POST){
            $codes = I('codes');
            $codes = explode("\r\n", $codes);
            $codes = array_filter($codes);
            if (!$codes){
                $this->error('未导入任何激活码');
            }
            foreach ($codes as $code) {
                $cdkeys[] = [
                    'goods_id' => $goods_id,
                    'code'     => $code
                ];
            }
            
            $res = $this->model->addAll($cdkeys);
             
            $res ? $this->success('导入成功') : $this->error('导入失败: 激活码有重复的');

        }
        else{
            $this->assign('goods_id', $goods_id);
            $this->display();
        }
    }


    public function _before_del($id)
    {
        $id = array_unique((array) I('id', 0));
        $id = is_array($id) ? array_map('intval', $id) : intval($id);
        $map['id'] = array('in', $id);
        $codes = $this->model->where($map)->select();
        foreach ($codes as $code) {
            if ($code['status'] != 0){
                $this->error('激活码处于已购买状态 不能删除');
            }
        }
    }

    

    

}